/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package game.module.Exploration;
import android.hardware.SensorManager;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import game.nm.SceneLoader;
import java.util.ArrayList;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.background.IBackground;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;
/**
 *
 * @author cow
 */
public class Level implements SceneLoader{
	//Varblock------------------------------
	private int playerStartX, playerStartY;
	private int mapXSize, mapYSize;
	private ColorBackground bgIm;
	private ArrayList<Sprite> sprList;
	private ArrayList<Body> bodyList;
	private Scene field;
	private String levelName;
	//======================================
	//physics-------------------------------
	private PhysicsWorld baseLevel;
	//======================================
	//image resources-----------------------
	private ArrayList<Texture> texs;
	private ArrayList<TextureRegion> imgs;
	//======================================

	/**
	 * Just a stub for the timr being
	 * @param str the name of the level file
	 */
	public Level(String str){
		playerStartX = 100;
		playerStartY = 200;
		mapXSize = 720;
		mapYSize = 480;
		baseLevel = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), true);
		bgIm = new ColorBackground(0, 255, 0);
		sprList = new ArrayList<Sprite>();
		texs = new ArrayList<Texture>();
		imgs = new ArrayList<TextureRegion>();
		field = null;
		levelName = str;
	}


	public void placePlayer(CharacterRepresentation cr, int eNum){
		if(!useable()) return;
		final FixtureDef pcdf = PhysicsFactory.createFixtureDef(1, 0f, 1);
		final Body pcB = PhysicsFactory.createBoxBody(baseLevel, cr.getRep(), BodyType.DynamicBody, pcdf);
		baseLevel.registerPhysicsConnector(new PhysicsConnector(cr.getRep(), pcB, true, true));
		cr.setBody(pcB);
		field.attachChild(cr.getRep());
	}
//Getters for level data-----------------------------------
	/**
	 * @return returns if the level is useable
	 */
	public boolean useable(){
		return field != null;
	}
	/**
	 * @return returns the players starting x
	 */
	public int getPlayerStartX(){
		return playerStartX;
	}
	/**
	 * @return returns the players starting y
	 */
	public int getPlayerStartY(){
		return playerStartY;
	}

	/**
	 * @return returns the map x dimension
	 */
	public int getMapSizeX(){
		return mapXSize;
	}
	/**
	 * @return returns the map y dimension
	 */
	public int getMapSizeY(){
		return mapYSize;
	}

	public String getLevelName(){
		return levelName;
	}

	public Scene LoadScene(BaseGameActivity BGA, Camera cam){
		if(field != null) return field;
		field = new Scene(2);
		field.setBackground(bgIm);
		loadResources(BGA);
		field.registerUpdateHandler(baseLevel);
		return field;
	}
	
	private void loadResources(BaseGameActivity BGA){
		final Shape center = new Rectangle(20, 300, mapXSize - 40, 10);
		center.setColor(0, 0, 0);
		final Shape platf = new Rectangle(mapXSize - 100, 280, 80, 10);
		platf.setColor(0, 0, 255);
		FixtureDef fdf = PhysicsFactory.createFixtureDef(0, 0f, 1f);
		PhysicsFactory.createBoxBody(baseLevel, center, BodyType.StaticBody, fdf);
		fdf = PhysicsFactory.createFixtureDef(0, 0, 1);
		PhysicsFactory.createBoxBody(baseLevel, platf, BodyType.StaticBody, fdf);
		field.attachChild(center);
		field.attachChild(platf);
	}
//=========================================================
}